package com.yc.bigdata.common.vo;


import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import com.yc.bigdata.common.constant.enums.ErrorCode;

import java.io.*;
import java.util.Map;

/**
 * <p>响应体</p>
 *
 * @author YuanChilde
 * @version 1.0
 * Modification History:
 * Date    Author      Version     Description
 * -----------------------------------------------------------------
 * 2019-09-18 17:25    YuanChilde     1.0        新增
 * @date 2019-09-18 17:25
 */
public class ResultBody<T> implements Serializable {

    private static final long serialVersionUID = 7503563788576518324L;

    /**
     * 响应编码
     */
    private int code = 0;
    /**
     * 提示消息
     */
    private String message;
    /**
     * 请求路径
     */
    private String path;
    /**
     * 响应数据
     */
    private T data;
    /**
     * http状态码
     */
    private int httpStatus;
    /**
     * 附加数据
     */
    private Map<String, Object> extra;

    public ResultBody() {
        super();
    }

    public static ResultBody ok() {
        return new ResultBody().code(ErrorCode.OK.getCode()).msg(ErrorCode.OK.getMessage());
    }

    public static ResultBody failed() {
        return new ResultBody().code(ErrorCode.FAIL.getCode()).msg(ErrorCode.FAIL.getMessage());
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public String getPath() {
        return path;
    }

    public T getData() {
        return data;
    }

    public Map<String, Object> getExtra() {
        return extra;
    }

    @JSONField(serialize = false, deserialize = false)
    @JsonIgnore
    public int getHttpStatus() {
        return httpStatus;
    }

    @JSONField(serialize = false, deserialize = false)
    @JsonIgnore
    public boolean isOk() {
        return this.code == ErrorCode.OK.getCode();
    }

    public ResultBody code(int code) {
        this.code = code;
        return this;
    }

    public ResultBody msg(String message) {
        this.message = message;
        return this;
    }

    public ResultBody data(T data) {
        this.data = data;
        return this;
    }

    public ResultBody path(String path) {
        this.path = path;
        return this;
    }

    public ResultBody httpStatus(int httpStatus) {
        this.httpStatus = httpStatus;
        return this;
    }

    public ResultBody put(String key, Object value) {
        if (this.extra == null) {
            this.extra = Maps.newHashMap();
        }
        this.extra.put(key, value);
        return this;
    }

    @Override
    public String toString() {
        return "ResultBody{" + "code=" + code + ", message='" + message + '\'' + ", path='" + path + '\'' + ", data=" + data + ", " +
                "httpStatus=" + httpStatus + ", extra=" + extra + '}';
    }


}
